NOAA Data:
US Fisheries and Wildlife Data:
Both data sets have geographic coordinates for ever observation
NOAA data is a .rdata file so we need to read it specially:
getwd() command to find your current working directoryload("noaa.rdata")Lets take a peek at the top of the floats NOAA data.
glimpse(floats)## Observations: 10,332
## Variables: 14
## $ callSign <fctr> Q4901043, Q4901043, Q4901043, Q4901043, Q49010...
## $ Date_Time <fctr> 7/12/2010, 7/12/2010, 7/12/2010, 7/12/2010, 7/...
## $ JulianDay <dbl> 2455390, 2455390, 2455390, 2455390, 2455390, 24...
## $ Time_QC <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ Latitude <dbl> 24.823, 24.823, 24.823, 24.823, 24.823, 24.823,...
## $ Longitude <dbl> -87.964, -87.964, -87.964, -87.964, -87.964, -8...
## $ Position_QC <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ Depth <int> 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,...
## $ Depth_QC <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ Temperature <dbl> 29.83, 29.65, 29.53, 29.49, 29.46, 29.44, 29.41...
## $ Temperature_QC <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ Salinity <dbl> 36.59, 36.58, 36.58, 36.58, 36.58, 36.58, 36.57...
## $ Salinity_QC <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ Type <fctr> Float, Float, Float, Float, Float, Float, Floa...
qplot(Longitude, Latitude, colour = callSign, data = floats) +
coord_map()qplot(Longitude, Latitude, colour = callSign, data = gliders) +
coord_map()qplot(Longitude, Latitude, colour = callSign, data = boats) +
coord_map()This data has the same context - a common time and common place
ggplot2 we will superimpose data onto this grid in layersTo give you an idea…
ggplot() +
geom_sf(data = states) +
geom_point(data = floats, aes(x = Longitude, y = Latitude, colour = callSign)) +
geom_point(aes(x, y), shape = "x", size = 5, data = rig) +
geom_text(aes(x, y), label = "BP Oil Rig",
size = 5, data = rig, hjust = -0.1) +
xlim(c(-91, -80)) + ylim(c(22,32))| aesthetic | mapping |
|---|---|
| x | Longitude |
| y | Latitude |
| color | CallSign |
| aesthetic | scale |
|---|---|
| x | continuous |
| y | continuous |
| color | discrete |
Facetting: None
qplot() vs ggplot()qplot() stands for “quickplot”:
ggplot() stands for “grammar of graphics plot”
qplot() and ggplot() for FloatsTwo ways to construct the same plot for float locations:
qplot(Longitude, Latitude, colour = callSign, data = floats) Or:
ggplot(data = floats,
aes(x = Longitude, y = Latitude, colour = callSign)) +
geom_point() +
scale_x_continuous() +
scale_y_continuous() +
scale_colour_discrete()We fortunately don’t need to be so verbose - Even ggplot() will automatically pick default scales:
ggplot(data = floats,
aes(x = Longitude, y = Latitude, colour = callSign)) +
geom_point()Find the ggplot() statement that creates this plot:
A layer added ggplot() can be a geom…
… or a position adjustment to the scales
| Plot | Geom | Stat |
|---|---|---|
| Scatterplot | point | identity |
| Histogram | bar | bin count |
| Smoother | line + ribbon | smoother function |
| Binned Scatterplot | rectange + color | 2d bin count |
More geoms described at http://docs.ggplot2.org/current/
Want to build a map using NOAA data
ggplot() +
geom_sf(data = states) +
geom_point(data = floats, aes(x = Longitude, y = Latitude, colour = callSign)) +
geom_point(aes(x, y), shape = "x", size = 5, data = rig) +
geom_text(aes(x, y), label = "BP Oil Rig", size = 5, data = rig, hjust = -0.1) +
xlim(c(-91, -80)) +
ylim(c(22, 32))animal <- read.csv("http://heike.github.io/rwrks/02-r-graphics/data/animal.csv", stringsAsFactors = FALSE)